package com.example.file.slice;

import com.example.file.ResourceTable;
import com.example.file.Utils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.app.Environment;

import java.io.*;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {

    private Button mBtnLocalDir;
    private Button mBtnLocalWrite;
    private Button mBtnLocalRead;
    private Button mBtnDistributedWrite;
    private Button mBtnDistributedRead;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        mBtnLocalDir = (Button) findComponentById(ResourceTable.Id_btn_dir);
        mBtnLocalDir.setClickedListener(this);
        mBtnLocalWrite = (Button) findComponentById(ResourceTable.Id_btn_local_write);
        mBtnLocalWrite.setClickedListener(this);
        mBtnLocalRead = (Button) findComponentById(ResourceTable.Id_btn_local_read);
        mBtnLocalRead.setClickedListener(this);
        mBtnDistributedWrite = (Button) findComponentById(ResourceTable.Id_btn_distributed_write);
        mBtnDistributedWrite.setClickedListener(this);
        mBtnDistributedRead = (Button) findComponentById(ResourceTable.Id_btn_distributed_read);
        mBtnDistributedRead.setClickedListener(this);
    }

    @Override
    public void onClick(Component component) {

        if (component.getId() == ResourceTable.Id_btn_dir) {

            // 沙盒目录
            Utils.log("getDataDir: " + getDataDir().toString());
            // 沙盒缓存目录
            Utils.log("getCacheDir: " + getCacheDir().toString());
            // 沙盒代码缓存目录
            Utils.log("getCodeCacheDir: " + getCodeCacheDir().toString());
            // 沙盒文件目录
            Utils.log("getFilesDir: " + getFilesDir().toString());
            // 外部存储私有区域 缓存目录
            Utils.log("getExternalCacheDir: " + getExternalCacheDir().toString());
            // 外部存储私有区域 files/output目录
            Utils.log("getExternalFilesDir: " + getExternalFilesDir("output").toString());

            // 分布式存储目录
            Utils.log("getDistributedDir: " + getDistributedDir().toString());

        }

        if (component.getId() == ResourceTable.Id_btn_local_write) {

            try {
                // 将文件存储在沙盒中
                // write(getFilesDir() + "/example.txt");
                // 将文件存储在外部存储私有区域 files目录中
                write(getExternalFilesDir("output") + "/example.txt");

            } catch (Exception e) {
                Utils.log(e.getLocalizedMessage());
            }
        }
        if (component.getId() == ResourceTable.Id_btn_local_read) {

            try {
                // 获取沙盒中的文本文件的数据
                // String content = read(getFilesDir() + "/example.txt");
                // 获取外部存储私有区域 files目录中的文本文件的数据
                String content = read(getExternalFilesDir("output") + "/example.txt");
                Utils.showToast(this, content);
            } catch (Exception e) {
                Utils.log(e.getLocalizedMessage());
            }
        }
        if (component.getId() == ResourceTable.Id_btn_distributed_write) {

            try {
                // 将文件存储在分布式文件系统中
                write(getDistributedDir() + "/example.txt");
            } catch (Exception e) {
                Utils.log(e.getLocalizedMessage());
            }
        }
        if (component.getId() == ResourceTable.Id_btn_distributed_read) {

            try {
                // 获取分布式文件系统中的文本文件的数据
                String content = read(getDistributedDir() + "/example.txt");
                Utils.showToast(this, content);
            } catch (Exception e) {
                Utils.log(e.getLocalizedMessage());
            }
        }
    }



    /**
     * 读文本文件内容
     * @param path 文件路径
     * @return 文件文本内容
     * @throws IOException IO异常
     */
    private static String read(String path) throws IOException {
        // 获得BufferedReader对象
        BufferedReader br = new BufferedReader(new FileReader(path));
        // 获取文本内容
        String content = "";
        String line;
        while ((line = br.readLine()) != null) {
            content += line;
        }
        br.close(); //关闭BufferedReader对象
        return content;
    }

    /**
     * 写文本文件内容
     * @param path 文件路径
     * @throws IOException IO异常
     */
    private static void write(String path) throws IOException {
        // 获得BufferedWriter对象
        BufferedWriter bw = new BufferedWriter(new FileWriter(path));
        bw.write("测试数据"); // 写字符串
        bw.newLine();  //换行
        bw.close(); //关闭BufferedWriter
    }

}
